
#include <stdio.h>
#include <time.h>
#include <amanita/aMath.h>
#include <amanita/aHashtable.h>
#include <amanita/aVector.h>
#include "../Main.h"
#include "../ProfileDlg.h"
#include "../Profile.h"
#include "TarotDeck.h"
#include "TarotStar.h"
#include "TarotDivinationDlg.h"
#include "TarotDivinationPage.h"



gboolean TarotDivinationDlg::delete_event_callback(GtkWidget *widget,GtkWidget *event,gpointer data) {
	TarotDivinationDlg *dlg = (TarotDivinationDlg *)data;
	delete dlg;
	return TRUE;
}

void TarotDivinationDlg::select_profile_clicked_callback(GtkWidget *widget,gpointer data) {
}

void TarotDivinationDlg::create_profile_clicked_callback(GtkWidget *widget,gpointer data) {
}

void TarotDivinationDlg::ok_clicked_callback(GtkWidget *widget,gpointer data) {
	TarotDivinationDlg *dlg = (TarotDivinationDlg *)data;
	dlg->openDivinationPage();
	delete dlg;
}

void TarotDivinationDlg::cancel_clicked_callback(GtkWidget *widget,gpointer data) {
	TarotDivinationDlg *dlg = (TarotDivinationDlg *)data;
	delete dlg;
}

TarotDivinationDlg::TarotDivinationDlg(GtkWindow *parent) {
//	TarotDeck *deck;
	sel = 0,cat = 0;
	deckSel = 0,starSel = 0;

	int i;
	GtkWidget *table;
	GtkWidget *frame;
	GtkWidget *widget;
	GtkWidget *vbox,*hbox;

	gtk.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(gtk.window),app.get("title.tarotdivdlg"));
	gtk_window_set_transient_for(GTK_WINDOW(gtk.window),parent);
//	gtk_widget_set_size_request(window,250,180);
	gtk_window_set_resizable(GTK_WINDOW(gtk.window),FALSE);
	gtk_window_set_position(GTK_WINDOW(gtk.window),GTK_WIN_POS_CENTER);
//	gtk_window_set_icon(GTK_WINDOW(window),app.createPixbuf(IMAGES_PATH "icons/icon16.png"));
	gtk_container_set_border_width(GTK_CONTAINER(gtk.window),5);

	table = gtk_table_new(2,3,FALSE);
	{
		frame = gtk_frame_new(app.get("profile"));
		vbox = gtk_vbox_new(FALSE,2);
		{
			gtk.profile = gtk_label_new(aString::blank);
			gtk_misc_set_alignment(GTK_MISC(gtk.profile),0,0.5);
			gtk_box_pack_start(GTK_BOX(vbox),gtk.profile,FALSE,FALSE,2);

			hbox = gtk_hbox_new(FALSE,2);
			widget = gtk_button_new_with_label(app.get("profile.select"));
			g_signal_connect(G_OBJECT(widget),"clicked",G_CALLBACK(select_profile_clicked_callback),this);
			gtk_widget_set_size_request(widget,80,-1);
			gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,2);
			widget = gtk_button_new_with_label(app.get("profile.create"));
			g_signal_connect(G_OBJECT(widget),"clicked",G_CALLBACK(create_profile_clicked_callback),this);
			gtk_widget_set_size_request(widget,80,-1);
			gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,2);
			gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,2);
		}
		gtk_container_add(GTK_CONTAINER(frame),vbox);
		gtk_table_attach(GTK_TABLE(table),frame,0,1,0,1,
			(GtkAttachOptions)(GTK_FILL|GTK_EXPAND),(GtkAttachOptions)(GTK_FILL|GTK_SHRINK),2,2);
	}

	{
		frame = gtk_frame_new(app.get("tarot.mixing"));
		vbox = gtk_vbox_new(FALSE,2);
		{
			for(i=0; i<3; i++) {
				gtk.mix[i] = i==0? gtk_radio_button_new_with_label(NULL,app.get("tarot.mix.1")) :
					gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(gtk.mix[0]),app.getf("tarot.mix.%d",i+1));
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk.mix[i]),FALSE);
				gtk_box_pack_start(GTK_BOX(vbox),gtk.mix[i],FALSE,FALSE,2);
			}
		}
		gtk_container_add(GTK_CONTAINER(frame),vbox);
		gtk_table_attach(GTK_TABLE(table),frame,0,1,1,2,
			(GtkAttachOptions)(GTK_FILL|GTK_EXPAND),(GtkAttachOptions)(GTK_FILL|GTK_SHRINK),2,2);
	}

	{
		frame = gtk_frame_new(app.get("tarot.deck"));
		vbox = gtk_vbox_new(FALSE,2);
		{
			gtk.tarotdeck = gtk_combo_box_text_new();
			gtk_box_pack_start(GTK_BOX(vbox),gtk.tarotdeck,FALSE,FALSE,2);

			gtk.tarotdeckimage = gtk_drawing_area_new();
			gtk_widget_set_size_request(gtk.tarotdeckimage,256,128);
//			g_signal_connect(G_OBJECT(gtk.tarotdeckimage),"configure_event",G_CALLBACK(deck_configure_event_callback),this);
//			g_signal_connect(G_OBJECT(gtk.tarotdeckimage),"expose_event",G_CALLBACK(deck_expose_event_callback),this);
			gtk_box_pack_start(GTK_BOX(vbox),gtk.tarotdeckimage,FALSE,FALSE,2);
		}
		gtk_container_add(GTK_CONTAINER(frame),vbox);
		gtk_table_attach(GTK_TABLE(table),frame,1,2,0,1,
			(GtkAttachOptions)(GTK_FILL|GTK_EXPAND),(GtkAttachOptions)(GTK_FILL|GTK_SHRINK),2,2);
	}

	{
		frame = gtk_frame_new(app.get("tarot.star"));
		vbox = gtk_vbox_new(FALSE,2);
		{
			gtk.tarotstar = gtk_combo_box_text_new();
			gtk_box_pack_start(GTK_BOX(vbox),gtk.tarotstar,FALSE,FALSE,2);

			gtk.tarotstarimage = gtk_drawing_area_new();
			gtk_widget_set_size_request(gtk.tarotstarimage,256,128);
//			g_signal_connect(G_OBJECT(gtk.tarotstarimage),"configure_event",G_CALLBACK(star_configure_event_callback),this);
//			g_signal_connect(G_OBJECT(gtk.tarotstarimage),"expose_event",G_CALLBACK(star_expose_event_callback),this);
			gtk_box_pack_start(GTK_BOX(vbox),gtk.tarotstarimage,FALSE,FALSE,2);
		}
		gtk_container_add(GTK_CONTAINER(frame),vbox);
		gtk_table_attach(GTK_TABLE(table),frame,1,2,1,2,
			(GtkAttachOptions)(GTK_FILL|GTK_EXPAND),(GtkAttachOptions)(GTK_FILL|GTK_SHRINK),2,2);
	}

	{
		hbox = gtk_hbox_new(FALSE,2);
		widget = gtk_button_new_with_label(app.get("ok"));
		g_signal_connect(G_OBJECT(widget),"clicked",G_CALLBACK(ok_clicked_callback),this);
		gtk_widget_set_size_request(widget,80,-1);
		gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,2);
		widget = gtk_button_new_with_label(app.get("cancel"));
		g_signal_connect(G_OBJECT(widget),"clicked",G_CALLBACK(cancel_clicked_callback),this);
		gtk_widget_set_size_request(widget,80,-1);
		gtk_box_pack_start(GTK_BOX(hbox),widget,FALSE,FALSE,2);
		gtk_table_attach(GTK_TABLE(table),hbox,0,2,2,3,
			(GtkAttachOptions)(GTK_FILL|GTK_EXPAND),(GtkAttachOptions)(GTK_FILL|GTK_SHRINK),2,2);
	}

	gtk_container_add(GTK_CONTAINER(gtk.window),table);
	gtk_widget_show_all(gtk.window);

//	deck = (TarotDeck *)(*tarotDecksList)[0];
//	char str[256];
//	sprintf(str,"images\\tarot\\tarot.%s.%s.fb.bmp",deck->file(),deck->language());
//	frontImage = backImage = (GdkPixbuf *)imageCache->get(str);
}

//bool TarotDivinationDlg::command(WPARAM wParam,LPARAM lParam) {
//	switch(LOWORD(wParam)) {
//		case IDC_TARDIV_PROFILE:if(HIWORD(wParam)==BN_CLICKED) ProfileDlg::show(this,profileDlgFunction);return true;
//		case IDOK:
//			if(HIWORD(wParam)==BN_CLICKED) {
//				char str[64] = { 0 };
//				HWND hwnd = getHWND();
//				HWND hcb = GetDlgItem(hwnd,IDC_TARDIV_NAME);
//				SendMessage(hcb,WM_GETTEXT,64,(LPARAM)str);
///*				if(*str) {
//					sel = (Profile *)chartData->get(str);
//					if(!sel) {
//						char msg[256];
//						sprintf(msg,"Namnet \"%s\" finns inte i databasen, vill du skriva in en ny kontakt?",str);
//						int n = MessageBox(hwnd,msg,"Ny Kontakt?",MB_YESNOCANCEL|MB_ICONQUESTION);
//						if(n==IDCANCEL) return false;
//						else if(n==IDYES) {
//							ProfileDlg *cd = new ProfileDlg(hwnd,str);
//							n = cd->result();
//							sel = cd->getProfile();
//							Window::closeWindow(cd);
//							if(n==IDCANCEL) return false;
//						}
//					}
//				}*/
//				hcb = GetDlgItem(hwnd,IDC_TARDIV_MIX);
//				int spread = SendMessage(hcb,CB_GETCURSEL,0,0);
//fprintf(out,"new TarotDivinationMDI(spread=%d)\n",spread);
//fflush(out);
//				new TarotDivinationMDI(sel,"thoth","keltisk",spread);
//				closeDlg(IDOK);
//				return true;
//			}
//		case IDCANCEL:if(HIWORD(wParam)==BN_CLICKED) closeDlg(IDCANCEL);return true;
//	}
//	return false;
//}

TarotDivinationDlg::~TarotDivinationDlg() {
	gtk_widget_destroy(gtk.window);
}

void TarotDivinationDlg::selectProfile(Profile &p) {
	profile = &p;
}

void TarotDivinationDlg::openDivinationPage() {
	int n;
	for(n=0; n<3; n++) if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(gtk.mix[n]))==TRUE) break;
	if(n==3) n = 0;
	new TarotDivinationPage(0,"waitesmith1910","celtcross",n);
}



